package com.prs.crm.action.crm;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.prs.crm.context.AppContext;
import com.prs.crm.domain.crm.CustomerSupplier;
import com.prs.crm.domain.crm.SupplierFoods;
import com.prs.crm.domain.hr.Person;
import com.prs.crm.service.crm.SupplierFoodsService;

public class SupplierFoodsAction extends BaseAction{
	private static final long serialVersionUID = 1L;
	
	private SupplierFoods supplierFoods;
	private CustomerSupplier customerSupplier;
	private SupplierFoodsService supplierFoodsService;
	private PaginationSupport pagination;
	private Integer[] foodIds;
	
	@Action("listSupplierFoods")
	public String listCustomerSupplier(){
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				SupplierFoods.class);
		criteria.add(Restrictions.or(Restrictions.like("sharePerson","%"+AppContext.getAccountContext().currentUser().getResume().getName()+"%"), Restrictions.like("person", "%"+AppContext.getAccountContext().currentUser().getResume().getName()+"%")));
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.supplierFoodsService.listByPage(criteria,
				pagination));
		return SUCCESS;
	}
	@Action("editSupplierFoods")
	public String editSupplierFoods(){
		this.supplierFoods=this.supplierFoodsService.get(foodIds[0]);
		this.customerSupplier=this.supplierFoods.getCustomerSupplier();
		return SUCCESS;
	}
	@Action("viewSupplierFoods")
	public String viewSupplierFoods(){
		this.supplierFoods=this.supplierFoodsService.get(foodIds[0]);
		this.customerSupplier=this.supplierFoods.getCustomerSupplier();
		return SUCCESS;
	}
	@Action(value = "removeSupplierFoods", results = {
			@Result(name = "success", type = "redirect", location = "listSupplierFoods"),
			@Result(name = "error", type = "chain", location = "listSupplierFoods")})
	public String removeSupplierFoods(){
		this.supplierFoodsService.batchRemove(foodIds);
		return SUCCESS;
	}
	@Action(value = "saveSupFoods", results = {
			@Result(name = "success", type = "redirect", location = "listSupplierFoods"),
			@Result(name = "error", type = "chain", location = "editSupplierFoods")})
	public String saveSupFoods(){
		this.supplierFoods.setCustomerSupplier(customerSupplier);
		this.supplierFoodsService.submit(supplierFoods);
		return SUCCESS;
	}
	
	public SupplierFoods getSupplierFoods() {
		return supplierFoods;
	}
	public void setSupplierFoods(SupplierFoods supplierFoods) {
		this.supplierFoods = supplierFoods;
	}
	public SupplierFoodsService getSupplierFoodsService() {
		return supplierFoodsService;
	}
	public void setSupplierFoodsService(SupplierFoodsService supplierFoodsService) {
		this.supplierFoodsService = supplierFoodsService;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public Integer[] getFoodIds() {
		return foodIds;
	}

	public void setFoodIds(Integer[] foodIds) {
		this.foodIds = foodIds;
	}
	public CustomerSupplier getCustomerSupplier() {
		return customerSupplier;
	}
	public void setCustomerSupplier(CustomerSupplier customerSupplier) {
		this.customerSupplier = customerSupplier;
	}
	
}
